<?php
include "func.php";
$m = explode(";",$_GET['m']);
$mode = $m[0];
if($mode == "list")
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Ramble Forums: Forum List</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<table class="list" id="forums">
    <thead>
        <tr>
            <th>Forum Name<br /><small>Forum Description</small></th>
            <th class="count">Topics</th>
            <th>Last Post</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="3" id="pages">
                Pages: 1
            </td>
        </tr>
    </tfoot>
    <tbody>
<?php
    foreach($api->q('SELECT forum_id FROM '. $config['mysql']['prefix'] .'forums') as $fid)
    {
        $forum = $api->query("forum",array("fid"=>$fid,"perpage"=>1));
        # Loop until you find the first one that has a last post
        $lp = $forum["topics"][0]["lp"];
        if(!$lp) $last = "No new posts.";
        else
        {
            $luser = $lp["user"];
            $last = date("F j, Y g:i:s A",$lp["date"]);
            $last .= '<br />by <a href="user.php?m=view;'.$luser["id"].'&amp;height=350&amp;width=450" class="dialog">'.$luser["name"].'</a>';
            if($lp["_type"] == "post")
            {
                $tid = $lp["tid"];
                $tname = $api->get("topic","title",$lp["tid"]);
                $last .= ' in <a href="topic/view/'.$tid.'">'.$tname.'</a>';
            }
            $last .= ".";
            
        }
?>
    <tr>
        <td><a href="forum/view/<?=$fid?>"><?=$forum["name"]?></a><?if($forum["desc"]){?><br /><small><?=$forum["desc"]?></small><?}?></td>
        <td class="count"><?=$forum['ntopics']?></td>
        <td><?=$last?></td>
    </tr>
<?php
    }
?>
    </tbody>
</table>
</body>
</html>
<?php
}
elseif($mode == "view")
{
    if(!$m[1])
    {
        echo "Please select a forum.";
        exit;
    }
    $fid = $m[1];
    $page = ($p = $_GET['page']) ? $p : 1;
    $pp = ($_GET['pp']) ? $_GET['pp'] : 15;
    $forum = $api->query("forum",array("fid"=>$fid,"page"=>$page,"pp"=>$pp));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>Ramble Forums: <?=$forum['name']?></title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<script type="text/javascript">
pp = <?=$pp?>;
</script>
<h1 class="list"><?=$forum['name']?></h1>
<div id="fnav">
<a href="forum.php?m=list">Forums</a> > <a href="forum.php?m=view;<?=$fid?>"><?=$forum['name']?></a>
</div>
<table class="list" id="topics">
    <thead>
        <tr>
            <th>Topic Name<br /><small>Topic Description</small></th>
            <th>User</th>
            <th class="count">Replies</th>
            <th>Last Post</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td>
                <?if($_COOKIE['ramble_id']){?><a href="topic.php?m=new;<?=$fid?>&amp;height=400&amp;width=500&amp;buttons=ntopic" class="dialog"><?=$i18n->get('nav/forums/ntopic')?></a><?}?>
            </td>
            <td colspan="3" id="pages">
            
            <?php
            $pages = ($p = $forum['pages']) ? $p : 1;
            pagelinks($fid,$pages,$page,"forum");
            ?>
            <form action="forum.php?m=view;<?=$fid?>&amp;page=<?=$page?>&amp;pp=<?=$pp?>&amp;order=asc" onsubmit="fp(<?=$fid?>,$('#jump').val(),<?=$sl?>); return false;" id="pjump">
                <div>Jump to page <input type="text" value="<?=$page?>" name="page" id="jump" /> of <?=$pages?></div>
            </form>
            </td>
        </tr>
    </tfoot>
    <tbody>
    <?php
        foreach($forum['topics'] as $topic)
        {
            $lp = $topic["lp"];
            if(!$lp) $last = "No new posts.";
            else
            {
                $luser = $lp["user"];
                $last = date("F j, Y g:i:s A",$lp["date"]);
                $last .= "<br />by <a href=\"user.php?m=view;{$luser["id"]}&amp;height=350&amp;width=450\" class=\"dialog\">{$luser["name"]}</a>.";
            }
    ?>
        <tr>
            <td><a href="topic/view/<?=$topic['id']?>"><?=$topic["title"]?></a><?if($topic["desc"]){?><br /><small><?=$topic["desc"]?></small><?}?></td>
            <td><a href="user.php?m=view;<?=$topic["user"]["id"]?>&amp;height=350&amp;width=450" class="dialog"><?=$topic["user"]["name"]?></a></td>
            <td class="count"><?=$topic['nposts']?></td>
            <td><?=$last?></td>
        </tr>
    <? } ?>
    </tbody>
</table>
</body>
</html>
<?php
}
?>